package com.cyou.nad.datastore.query;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cyou.nad.dao.AbstractPagination;
import com.cyou.nad.datastore.AbstractDataList;
import com.cyou.nad.datastore.ColType;
import com.cyou.nad.datastore.DBHeader;
import com.cyou.nad.datastore.DSRow;
import com.cyou.nad.datastore.DataList;




/**
 * 使用jdbc方式查找生成的page  通过getDataList() 获得结果结果集
 * @author lzm 
 */
@SuppressWarnings("serial")
public class JdbcPage extends AbstractPagination implements IJdbcPage {
	
	public static void main(String[] args) {
		JdbcPageDataList dataList = new JdbcPageDataList();
		System.out.println(dataList.rowCount());
		System.out.println(dataList.colCount());
	}

	DataList data = null; // 当前页中存放的记录,类型一般为List

	/**
	 * 构造方法，只构造空页.
	 */
	public JdbcPage() {
		this(0, 0, DEFAULT_PAGE_SIZE, new JdbcPageDataList());
	}

	/**
	 * 默认构造方法.
	 * 
	 * @param start
	 *            本页数据在数据库中的起始位置
	 * @param totalSize
	 *            数据库中总记录条数
	 * @param pageSize
	 *            本页容量
	 * @param data
	 *            本页包含的数据
	 */
	public JdbcPage(int start, int totalSize, int pageSize, DataList data) {
		this.pageSize = pageSize;
		this.start = start;
		totalCount = totalSize;
		this.data = data;
	}


	public DataList getDataList() {
		if (data == null)
			//			throw new IllegalStateException("没有给page 添加数据");
			data = new JdbcPageDataList();
		return data;
	}

	

	public String toJson() {
		DataList dataList = getDataList();
		if (dataList.rowCount() == 0) {
			return "{totalProperty:0,root:[]}";
		}
		int colCount = dataList.colCount();
		int rowCount = dataList.rowCount();
		StringBuffer sb = new StringBuffer("{totalProperty:").append(getTotalCount()).append(",root:[");
		for (int i = 0; i < rowCount; i++) {
			sb.append("{");
			for (int j = 0; j < colCount; j++) {
				String headName = dataList.getHeaders().get(j).getColDBName();
				sb.append(headName).append(":").append("'").append(dataList.getObject(i, j)).append("',");

			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append("},");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("]}");
		return sb.toString();
	}

}

@SuppressWarnings("serial")
class JdbcPageDataList extends AbstractDataList {
	public JdbcPageDataList() {
		super();
	}

	public JdbcPageDataList(ResultSet rs) throws SQLException {
		super();
		//列数
		int colCount = dealHead(rs);
		dealBody(rs, colCount);
	}

	@SuppressWarnings("unchecked")
	private void dealBody(ResultSet rs, int colCount) throws SQLException {
		int rowNum = 0;
		while (rs.next()) {
			if (rs.getRow() == 0) {
				break;
			}
			DSRow row = new DSRow(colCount);
			rowList.add(row);
			for (int col = 1; col <= colCount; col++) {
				Object obj = ColType.getResultSetValue(rs, col, getColType(col - 1));
				addCell(rowNum, col - 1, (Comparable) obj);
			}
			rowNum++;
		}
	}

	private int dealHead(ResultSet rs) throws SQLException {
		ResultSetMetaData resultSetMetaData = rs.getMetaData();
		int colCount = resultSetMetaData.getColumnCount();
		headerList = new ArrayList<DBHeader>(colCount);
		for (int col = 1; col <= colCount; col++) {
			int colType = resultSetMetaData.getColumnType(col);//获取此列的数据类型
			DBHeader header = new DBHeader();
			ColType type = ColType.parseSqlType(colType);
			header.setColType(type);

			header.setColDBName(resultSetMetaData.getColumnLabel(col));//返回此列暗含的标签
			headerList.add(header);
		}
		return colCount;
	}

}